﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using Services;
using Utilities.Menu;
using System.Collections.ObjectModel;

using Services.Menu;
using Utilities;

using SuperTicTacToe.Menu;
//using BowMaster.Levels;

namespace SuperTicTacToe
{
    public class StateManager : AStateManager
    {
        Game game;
        public GraphicsDeviceManager graphics;
        MainMenu mainMenu;
        HighScoresMenu highScoresMenu;
        ConfigMenu configMenu;
        CreditsMenu creditsMenu;
        Level2D level2d;
        Level3D level3d;
        //public ArrayList<ALevel> levels;
        //public GameState GameState;
        

        //SoundTrackComponent stcomp;
        public StateManager(Game game, GraphicsDeviceManager graphics)
            : base(game, graphics)
        {
            this.game = game;
            this.graphics = graphics;

            //GameState = Storage.GetGameState();
            
            ////for (int i = 0; i < GameState.Records.Count; i++)
            ////{
            ////    levels[i].recordTime = GameState.Records[i];
            ////    levels[i].Available = GameState.Available[i];
            ////}
            ////stcomp = new SoundTrackComponent(game);
            mainMenu = new MainMenu(game, this);
            highScoresMenu = new HighScoresMenu(game, this);
            configMenu = new ConfigMenu(game, this);
            creditsMenu = new CreditsMenu(game, this);
        }


        public override void SelectMainMenu()
        {
            //Storage.SaveGameState(GameState);
            CleanupComponents();
            //game.Components.Add(new Level(game, game.GraphicsDevice));
            mainMenu=new MainMenu(game, this);
            game.Components.Add(mainMenu);
            GC.Collect();
            GC.WaitForPendingFinalizers();

#if WINDOWS || XBOX

            graphics.PreferredBackBufferWidth = 480;
            graphics.PreferredBackBufferHeight = 800;
            graphics.ApplyChanges();

#else
            graphics.PreferredBackBufferWidth = 480;
            graphics.PreferredBackBufferHeight = 800;
            graphics.SupportedOrientations = DisplayOrientation.Portrait;
            graphics.ApplyChanges();

#endif
        }

        public void SelectHighScoresMenu()
        {
           
            game.Components.Remove(mainMenu);
            mainMenu.DisposeMenu();
            highScoresMenu = new HighScoresMenu(game, this);
            game.Components.Add(highScoresMenu);
        }

        public void SelectConfigMenu()
        {
            game.Components.Remove(mainMenu);
            mainMenu.DisposeMenu();
            configMenu = new ConfigMenu(game, this);
            game.Components.Add(configMenu);
        }
        public void SelectCreditsMenu()
        {
            
            game.Components.Remove(mainMenu);
            mainMenu.DisposeMenu();
            creditsMenu = new CreditsMenu(game, this);
            game.Components.Add(creditsMenu);
        }
        public void StartGame2D(ArrayList<Player> players, int size)
        {
            
            game.Components.Remove(configMenu);
            configMenu.DisposeMenu();
            if (level2d != null)
                level2d.DisposeMenu();
            level2d = new Level2D(game, this, players, size);
            game.Components.Add(level2d);
        }
        public void StartGame3D(ArrayList<Player> players, int size)
        {
            
            game.Components.Remove(configMenu);
            configMenu.DisposeMenu();
            if (level3d != null)
                level3d.DisposeMenu();
            level3d = new Level3D(game, this, players, size);
            game.Components.Add(level3d);
        }
//        public void SelectLevelSelectMenu()
//        {
//            CleanupComponents();
//            game.Components.Add(new LevelSelectMenu(game, graphics));
            
//            GC.Collect();
//            GC.WaitForPendingFinalizers();

//#if WINDOWS || XBOX

//            graphics.PreferredBackBufferWidth = 480;
//            graphics.PreferredBackBufferHeight = 800;
//            graphics.ApplyChanges();
            
//#else
//            graphics.PreferredBackBufferWidth = 480;
//            graphics.PreferredBackBufferHeight = 800;
//            graphics.SupportedOrientations = DisplayOrientation.Portrait;
//            graphics.ApplyChanges();

//#endif
//        }


//        public void SelectOptionsMenu()
//        {
//            CleanupComponents();
//            game.Components.Add(new OptionsMenu(game));

//            GC.Collect();
//            GC.WaitForPendingFinalizers();

//#if WINDOWS || XBOX

//            graphics.PreferredBackBufferWidth = 480;
//            graphics.PreferredBackBufferHeight = 800;
//            graphics.ApplyChanges();

//#else
//            graphics.PreferredBackBufferWidth = 480;
//            graphics.PreferredBackBufferHeight = 800;
//            graphics.SupportedOrientations = DisplayOrientation.Portrait;
//            graphics.ApplyChanges();

//#endif
//        }

//        public void SelectCreditsMenu()
//        {
//            CleanupComponents();

//            game.Components.Add(new CreditsMenu(game));
//            GC.Collect();
//            GC.WaitForPendingFinalizers();

//#if WINDOWS || XBOX

//            graphics.PreferredBackBufferWidth = 480;
//            graphics.PreferredBackBufferHeight = 800;
//            graphics.ApplyChanges();

//#else
//            graphics.PreferredBackBufferWidth = 480;
//            graphics.PreferredBackBufferHeight = 800;
//            graphics.SupportedOrientations = DisplayOrientation.Portrait;
//            graphics.ApplyChanges();
//#endif
//        }
//        public void SelectLevel(int index)
//        {
//            if (index < 0 || index >= levels.Count) return;

//            CleanupComponents();

//            if (game.IsTrial() && index>=8)
//            {
//                SelectCreditsMenu();
//                return;
//            }

//            GameState.Current_level = index;
            
//            levels[index].Reset();
            
//            game.Components.Add(levels[index]);
//            //GC.Collect();
//            //GC.WaitForPendingFinalizers();

//#if WINDOWS || XBOX

//            graphics.PreferredBackBufferWidth = 800;
//            graphics.PreferredBackBufferHeight = 480;
//            graphics.ApplyChanges();
            
//#else
//            graphics.PreferredBackBufferWidth = 800;
//            graphics.PreferredBackBufferHeight = 480;
//            graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft;
//            graphics.ApplyChanges();

//#endif
//        }

//        public void Resume()
//        {
//            SelectLevel(GameState.Current_level);
//        }

//        public void SelectNextLevel()
//        {
//            //levels[GameState.Current_level].Available = true;
//            levels[GameState.Current_level].level_ended = true;
//            int newRecord = levels[GameState.Current_level].shoots;
//            //newRecord = (float)Math.Round(newRecord, 2);
//            if (GameState.Records[GameState.Current_level] >= newRecord)
//                GameState.Records[GameState.Current_level] = newRecord;
//            GameState.Available[GameState.Current_level] = true;

//            if (GameState.Current_level + 1 >= levels.Count)
//                SelectCreditsMenu();
//            else
//            {
//                GameState.Current_level++;
//                Resume();
//            }
//            Storage.SaveGameState(GameState);
            
//        }
//        public void SelectPrevLevel()
//        {
//            levels[GameState.Current_level].level_ended = true;
//            if (GameState.Current_level - 1 >= 0)
//                GameState.Current_level--;
//            Storage.SaveGameState(GameState);
//            Resume();
//        }
//        public void ResetGameState()
//        {
//            GameState = Storage.ResetGameState();
//        }

        
    }
    
}
